package com.cabrida.livestar.message;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cabrida.livestar.R;
import com.cabrida.livestar.config.Global;
import com.cabrida.livestar.model.MsgLetterData;
import com.cabrida.livestar.util.MyUtils;
import com.cabrida.livestar.view.CircleImageView;

/**
 * 私信adapter
 * 
 * @author why
 * 
 */
public class LetterDetailsAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private ArrayList<MsgLetterData> data_list = new ArrayList<MsgLetterData>();

	public LetterDetailsAdapter(Context context) {
		this.mInflater = LayoutInflater.from(context);
	}

	public void setData(ArrayList<MsgLetterData> data) {
		data_list = data;
	}

	@Override
	public int getCount() {
		return data_list.size();
	}

	@Override
	public Object getItem(int arg0) {
		return data_list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		if (convertView == null) {

			holder = new ViewHolder();

			convertView = mInflater.inflate(R.layout.adapter_letter_details_lv_item,
					null);
			holder.iv_head_left = (CircleImageView) convertView.findViewById(R.id.iv_head_left);
			holder.ll_left = (LinearLayout) convertView.findViewById(R.id.ll_left);
			holder.tv_name_left = (TextView) convertView.findViewById(R.id.tv_name_left);
			holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
			holder.tv_lv_left = (TextView) convertView.findViewById(R.id.tv_lv_left);
			holder.tv_des_left = (TextView) convertView.findViewById(R.id.tv_des_left);
			holder.iv_head_right = (CircleImageView) convertView.findViewById(R.id.iv_head_right);
			holder.ll_right = (LinearLayout) convertView.findViewById(R.id.ll_right);
			holder.tv_name_right = (TextView) convertView.findViewById(R.id.tv_name_right);
			holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
			holder.tv_lv_right = (TextView) convertView.findViewById(R.id.tv_lv_right);
			holder.tv_des_right = (TextView) convertView.findViewById(R.id.tv_des_right);
			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();

		}
		
		holder.tv_time.setText(data_list.get(position).getC_createtime());
		
		if (Global.U_id.equals(data_list.get(position).getC_udid())) {
			holder.ll_left.setVisibility(View.GONE);
			holder.ll_right.setVisibility(View.VISIBLE);
			MyUtils.loadImg(data_list.get(position).getCavatar(), holder.iv_head_right);
			holder.tv_name_right.setText(data_list.get(position).getCnickname());
			holder.tv_des_right.setText(data_list.get(position).getC_content());
		}else {
			holder.ll_left.setVisibility(View.VISIBLE);
			holder.ll_right.setVisibility(View.GONE);
			MyUtils.loadImg(data_list.get(position).getCavatar(), holder.iv_head_left);
			holder.tv_name_left.setText(data_list.get(position).getCnickname());
			holder.tv_des_left.setText(data_list.get(position).getC_content());
//			holder.tv_lv_left.setText(data_list.get(position).getC_content());
		}
		

		return convertView;
	}

	class ViewHolder {
		private TextView tv_time;
		private LinearLayout ll_left;
		private CircleImageView iv_head_left;
		private TextView tv_name_left;
		private TextView tv_lv_left;
		private TextView tv_des_left;
		private LinearLayout ll_right;
		private CircleImageView iv_head_right;
		private TextView tv_name_right;
		private TextView tv_lv_right;
		private TextView tv_des_right;
	}

}
